<?php

namespace framework\system\kernel;

use framework\system\kernel\CFile;

/**
 * Description of CModle
 *
 * @author Administrator
 */
class CModel extends Object {

    protected $data = [];

    public function loadModel($model) {
        $path = explode('/', $model);
        $name = array_pop($path);
        $path = implode('/', $path);
        $modelClassName = ucwords(preg_replace('#[^a-zA-Z0-9]#', '', $name)) . "Model";
        $fileName = ucfirst($name);

        $file = DIR_APPLICATION . DS . 'model' . DS . $path . DS . $fileName . EXT;
        if (!CFile::fileExits($file)) {
            $file = DIR_FRAMEWORK . DS . 'model' . DS . $path . DS . $fileName . EXT;
        }

        CFile::requireFile($file);
        $model_obj = new $modelClassName;
        $modelName = preg_replace('#[^a-zA-Z0-9]#', '_', $model);
        $this->$modelName = $model_obj;
    }

    public function loadLang($lang) {
        $file = DIR_APPLICATION . DS . 'lang' . DS . $lang . EXT;
        return CFile::requireFile($file, TRUE);
    }

    public function __set($name, $value) {
        $this->data[$name] = $value;
    }

    public function __get($name) {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        } else {
            return NULL;
        }
    }

}
